package com.lwc.cfdns.controller.admin.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lwc.cfdns.constants.ConstantsUrl;
import com.lwc.cfdns.pojo.bo.system.DictBO;
import com.lwc.cfdns.pojo.dto.ValidGroup;
import com.lwc.cfdns.pojo.dto.system.DictDTO;
import com.lwc.cfdns.pojo.entity.SystemDict;
import com.lwc.cfdns.pojo.vo.system.DictVO;
import com.lwc.cfdns.service.impl.SystemDictServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 字典
 *
 * @author luwc
 * @title DictController
 * @description
 * @time 2023/5/28 20:45
 * @version: 1.0
 */
@RestController
@RequestMapping(ConstantsUrl.ADMIN + "/system")
public class DictController {
    @Autowired
    private SystemDictServiceImpl systemDictService;

    /**
     * 创建
     *
     * @param dictDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 20:59
     */
    @PreAuthorize("hasAnyAuthority('system:dict:create')")
    @PostMapping("/dict")
    public Map<String, Object> create(@Validated(ValidGroup.Create.class) @RequestBody DictDTO dictDTO) {
        return systemDictService.create(dictDTO);
    }

    /**
     * 修改
     *
     * @param dictDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 21:01
     */
    @PreAuthorize("hasAnyAuthority('system:dict:update')")
    @PutMapping("/dict")
    public Map<String, Object> update(@Validated({ValidGroup.Update.class}) @RequestBody DictDTO dictDTO) {
        return systemDictService.update(dictDTO);
    }

    /**
     * 详情
     *
     * @param id
     * @return DictVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 21:06
     */
    @PreAuthorize("hasAnyAuthority('system:dict:view')")
    @GetMapping("/dict/{id}")
    public DictVO view(@PathVariable("id") @NotNull(message = "ID不能为空") Long id) {
        return systemDictService.view(id);
    }

    /**
     * 删除
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 21:08
     */
    @PreAuthorize("hasAnyAuthority('system:dict:delete')")
    @DeleteMapping("/dict/{ids}")
    public Map<String, Object> delete(@PathVariable("ids") @NotNull(message = "ids不能为空") String ids) {
        return systemDictService.delete(ids);
    }

    /**
     * 列表
     *
     * @param dictBO
     * @return IPage<DictVO>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 21:10
     */
    @PreAuthorize("hasAnyAuthority('system:dict:lists')")
    @GetMapping("/dict")
    public IPage<SystemDict> lists(DictBO dictBO) {
        return systemDictService.lists(dictBO);
    }

    /**
     * 删除所有字典缓存
     *
     * @param
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/29 15:46:18
     */
    @PreAuthorize("hasAnyAuthority('system:dict:refreshCache')")
    @DeleteMapping("/dict/refreshCache")
    public Map<String, Object> refreshCache() {
        return systemDictService.refreshCache();
    }
}
